#pragma once
#include <iostream>
#include <string>
#include <unordered_map>
#include <functional>
#include <memory>
#include <unistd.h>
#include <time.h>
using namespace std;
const int numss = 0;
class Session
{
public:
    Session()
    {
        _username = "user-" + to_string(numss);
        _password = "123456";
    }
    ~Session()
    {
        _username = "";
        _password = "";
    }
    string _username;
    string _password;
};

class manageSession
{
public:
    manageSession()
    {
        srand(time(nullptr));
    }
    // 新增session对象
    void AddSession(string sessionid, shared_ptr<Session> s)
    {
        _manageSession.insert(make_pair(sessionid, s));
    }
    // 通过sessionid获取session对象指针
    shared_ptr<Session> GetSession(string sessionid)
    {
        auto pos = _manageSession.find(sessionid);
        if (pos == _manageSession.end())
        {
            return nullptr;
        }
        return _manageSession[sessionid];
    }
    // 传递一个session对象，返回一个sessionid
    string GetSessionid(shared_ptr<Session> s)
    {
        unsigned int pos = rand() + time(nullptr);
        string sessionid = to_string(pos);
        _manageSession.insert(make_pair(sessionid, s));
        return sessionid;
    }

private:
    unordered_map<string, shared_ptr<Session>> _manageSession;
};
